package cn.hj.user.service.impl;


import cn.hj.basic.constant.BaseConstant;
import cn.hj.basic.dto.UserDto;
import cn.hj.basic.exception.BusinessException;
import cn.hj.basic.service.impl.BaseServiceImpl;
import cn.hj.basic.util.MD5Utils;
import cn.hj.basic.util.StrUtils;
import cn.hj.user.domain.Logininfo;
import cn.hj.user.domain.User;
import cn.hj.user.mapper.LogininfoMapper;
import cn.hj.user.mapper.UserMapper;
import cn.hj.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.Key;
import java.security.PublicKey;
import java.util.Date;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 手机注册
     * @param dto
     */
    @Override
    public void registerPhone(UserDto dto) {
        //判断手机号不能不能为空
        if(StringUtils.isEmpty(dto.getPhone())||StringUtils.isEmpty(dto.getVerifyCode())||StringUtils.isEmpty(dto.getPassword())||StringUtils.isEmpty(dto.getConfirmPassword())){
            throw new BusinessException("请填写好信息");
        }
        //判断2次密码是否一致
        if (!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BusinessException("2次密码不一致，请重新输入");
        }
        //用户不能背注册
        User user = userMapper.loadByPhone(dto.getPhone());
        if (user!=null){
            throw new BusinessException("手机号已被注册，请直接登录");
        }
        //判断验证码是否过期
        String key = BaseConstant.BUSINESS_REGISTER + dto.getPhone();
        Object codeTmp = redisTemplate.opsForValue().get(key);
        if (codeTmp == null) {
            throw new BusinessException("验证码已失效请重写获取");
        }
        //判断验证码是否正确
        String  code=((String)codeTmp).split(":")[0];
        if(!dto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码错误，请重写输入");
        }
        //转类型
        User user1 = dto2User(dto);
        Logininfo logininfo = dto2Logininfo(user1);
        //保存数据先保存存loginifo,返回自增id,才存user表
        logininfoMapper.insert(logininfo);
        user1.setLogininfo_id(logininfo.getId());
        userMapper.insert(user1);

    }


    private User dto2User(UserDto dto){
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        //设置盐值
        String slat = StrUtils.getComplexRandomString(32);
        user.setSalt(slat);
        //加密
        String md5 = MD5Utils.encrypByMd5(dto.getPassword() + slat);
        user.setPassword(md5);
        return user;
    }

    private Logininfo dto2Logininfo(User user){
        Logininfo logininfo = new Logininfo();
        //通明拷贝
        BeanUtils.copyProperties(user,logininfo);
        return logininfo;
    }
}
